package cn.iocoder.yudao.module.member.vip.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.api.infrastructure.config.ConfigApiV2;
import cn.iocoder.yudao.module.api.infrastructure.config.dto.MemberConfigRespDTO;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.member.vip.service.VipCardService;
import cn.iocoder.yudao.module.member.vip.vo.VipCardVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.api.infrastructure.ConfigConstants.CONFIG_MEMBER;

@Tag(name = "用户 APP - 会员卡")
@RestController
@RequestMapping("/member/vip-card")
@Validated
public class AppVipCardController {

    @Resource
    private VipCardService vipCardService;

    @Resource
    private ShopInfoApi shopInfoApi;
    @Resource
    private ConfigApiV2 configApiV2;

    @GetMapping("/list")
    @Operation(summary = "获得会员卡列表")
    @Parameter(name = "limit", description = "数据条数", required = true, example = "3")
    @PermitAll
    public CommonResult<List<VipCardVO>> getVipCardList(@RequestParam(value = "limit", required = false, defaultValue = "3") Integer limit) {
        boolean isMerchant = shopInfoApi.isOwnerByMemberUserId(getLoginUserId());
        List<VipCardVO> list = vipCardService.getList(isMerchant, limit);
        int deductUnitPrice = configApiV2.getConfig(CONFIG_MEMBER, MemberConfigRespDTO.class).getPointTradeDeductUnitPrice();
        for (VipCardVO item : list) {
            item.setDeductPrice(deductUnitPrice * item.getGivePoint());
        }
        return success(list);
    }

}